﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Freecart_Local_.Classes;

namespace Freecart_Local_
{
    public static class CartManager
    {
        private static Dictionary<Guid, Cart> _carts = new Dictionary<Guid, Cart>();

        public static Cart FetchCartByGuid(Guid guid)
        {
            if (_carts.Keys.Contains(guid))
                return _carts[guid];

            return null;
        }

        public static void ClearCarts()
        {
            _carts.Clear();
        }

        public static IEnumerable<Cart> ReturnAllCarts()
        {
            List<Cart> _cartList = new List<Cart>();
            foreach (KeyValuePair<Guid, Cart> c in _carts)
            {
                _cartList.Add(c.Value);
            }

            return _cartList;
        }

        public static bool AddCart(Cart cart)
        {
            if (cart.Guid != null)
            {
                AddCart(cart.Guid, cart);
                return true;
            }
            else
            {
                return false;
            }
        }

        public static void AddCart(Guid guid, Cart cart)
        {
            _carts.Add(guid, cart);
        }

        public static bool isValidGuid(Guid guid)
        {
            if (guid != null && _carts.Keys.Contains(guid)) 
                return true;
            else 
                return false;
        }
    }
}
